package com.memoir.jpa.controller;

import com.memoir.jpa.model.TodoDto;
import com.memoir.jpa.service.TodoCrudService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * jpa注释在service层
 */
@RestController
@RequestMapping("/crud")
public class TodoCrudController {

    @Autowired
    private TodoCrudService todoCrudService;

    @RequestMapping(value = "/save/one",method = RequestMethod.POST)
    public TodoDto saveTodo(@RequestBody TodoDto todoDto){

        return todoCrudService.saveTodo(todoDto);
    }

    @RequestMapping(value = "/save/more",method = RequestMethod.POST)
    public List<TodoDto> saveTodos(@RequestBody List<TodoDto> todos){

        return todoCrudService.saveTodos(todos);
    }

    @RequestMapping(value = "/count",method = RequestMethod.GET)
    public Long count(){
        return todoCrudService.count();
    }

    @RequestMapping(value = "/exist/{id}",method = RequestMethod.GET)
    public Boolean exist(@PathVariable Integer id){
        return todoCrudService.exists(id);
    }

    @RequestMapping(value = "/delete/{id}",method = RequestMethod.GET)
    public void delete(@PathVariable Integer id){

        todoCrudService.delete(id);
    }
    @RequestMapping(value = "/delete/one",method = RequestMethod.DELETE)
    public void delete(@RequestBody TodoDto todo){

        todoCrudService.delete(todo);
    }
    @RequestMapping(value = "/delete/more",method = RequestMethod.DELETE)
    public void delete(@RequestBody List<TodoDto> todos){
        todoCrudService.delete(todos);
    }

    @RequestMapping(value = "/find",method = RequestMethod.GET)
    public List<TodoDto> findAll(){
        return todoCrudService.findAll();
    }

    @RequestMapping(value = "/find/more",method = RequestMethod.POST)
    public List<TodoDto> findAll(@RequestBody List<Integer> ids){
        return todoCrudService.findAll(ids);
    }
}
